<?php


namespace frontend\controllers\cyf;


use frontend\controllers\common;
use frontend\models\cyf\User;
use logic\cyf\LoginLogic;
//use servers\Common;
use yii\base\DynamicModel;
use yii\db\Query;
use yii\log\Logger;
use yii\web\Controller;
use yii\web\Request;

class LoginController extends Controller
{
    /**
     * 用户注册
     * @param Request $request
     * @return false|string
     */
    public function actionIegister(Request $request)
    {
        try {
            if($request->method != 'POST') return common::fail('20001','null','请求方式错误');
            $params = $request->post();
            //表单验证

            //注册
            $uId = LoginLogic::iegister($params);
            if($uId){
                //记录日志
                \Yii::info($uId.date('Y-m-d H:i:s').'用户注册');

                return  common::success(0,$uId,'注册成功');
            }

        }catch (\Exception $exception){
            return  common::fail('10001','null',$exception->getMessage());
        }


    }

    /**
     * 发送验证码
     * @param Request $request
     * @return false|string
     */
    public function actionSendsms(Request $request)
    {
        try {
            if($request->method != 'POST'){
                return common::fail('20001',null,'请求方式错误');
            }
            $phone = $request->post('phone');
            if(empty($phone)){
                return common::fail(10001,"",'手机号不能为空');
            }
            $res = LoginLogic::sendSms($phone);
            if($res){
                return common::success(0,$res,'发送验证码成功');
            }

        }catch (\Exception $exception){
            return common::fail(10002,"null",$exception->getMessage());
        }

    }

    /**
     * 设置密码
     * @param Request $request
     * @return false|string
     */
    public function actionSetPassword(Request $request)
    {
        try {
            if($request->method != 'POST'){
                return common::fail('20001',null,'请求方式错误');
            }
            $params = $request->post();
            if(empty($params['password'])){
                return common::fail('10001',null,'密码不能为空');
            }
            //验证密码规则

            //调用逻辑层
            $res = LoginLogic::setPassword($params);
            if($res){
                return common::success('0',null,'设置密码成功');
            }
        }catch (\Exception $exception){
            return common::fail('10003','null',$exception->getMessage());
        }
    }

    /**
     * 登录
     * @param Request $request
     * @return false|string
     */
    public function actionLogin(Request $request)
    {
        try {
            if($request->method != 'POST'){
                return common::fail('20001',null,'请求方式错误');
            }
            $params = $request->post();
            //表单验证

            //调用逻辑层登录逻辑
            $token = LoginLogic::login($params);
            if($token){
                \Yii::getLogger()->log('登录成功',Logger::LEVEL_INFO);
                return  common::success(0,$token,'登录成功');
            }
        }catch (\Exception $exception){
            return common::fail(10005,null,$exception->getMessage());
        }

    }


}